// pages/admin/waiting/waiting.js
const app = getApp()
var host = app.globalData.host;
var self = null;

Page({
  data: {
    themeColor: '',
    code: '',
    intervalId: '',
    interval: 10000,
    timeout: 30000
  },
  onLoad: function (options) {
    self = this;
    self.getStyle();
    self.setData({
      code: options.code
    })
    app.globalData.isTimeout = false;

    var intervalId = setInterval(function() {
      if (app.globalData.isTimeout) {
        clearInterval(self.data.intervalId);
        return;
      }
      self.checkResult();
    }, self.data.interval);
    self.setData({
      intervalId: intervalId
    })
    setTimeout(function() {
      app.globalData.isTimeout = true;
      self.reverse();

      wx.showModal({
        title: '提示',
        content: '用户支付超时',
        showCancel: false,
        success: res => {
          wx.navigateBack({
            delta: 1
          })
        }
      })
    }, self.data.timeout);
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor
    })
  },
  checkResult() {
    console.log("check result ...");
    var url = host + '/yq/api/query';
    var params = {
      code: self.data.code
    }
    var success = res => {
      var query = res.info;
      if (query.trade_state == 'SUCCESS') {
        wx.showModal({
          title: '提示',
          content: '用户支付成功',
          showCancel: false,
          success: res => {
            wx.navigateBack({
              delta: 1
            })
          }
        })
      } else if (query.trade_state == 'REVOKED' || query.trade_state == 'PAYERROR') {
        wx.showModal({
          title: '提示',
          content: '支付失败',
          showCancel: false,
          success: res => {
            wx.navigateBack({
              delta: 1
            })
          }
        })
      }
    }
    var fail = res => { }
    var comp = () => { }
    app.request(url, params, success, fail, comp);
  },
  cancel(e) {
    // var url = host + '/yq/api/close';
    // var params = {
    //   code: self.data.code
    // }
    // var success = res => {
    //   wx.showModal({
    //     title: '提示',
    //     content: '订单已取消',
    //     showCancel: false,
    //     success: res => {
    //       wx.navigateBack({
    //         delta: 1
    //       })
    //     }
    //   })
    // }
    // var fail = res => { }
    // var comp = () => { }
    // app.requestLoading(url, params, success, fail, comp);
    self.reverse();
  },
  reverse() {
    var url = host + '/yq/api/reverse';
    var params = {
      code: self.data.code
    }
    var success = res => {
      if (res.info.return_code == "SUCCESS" && res.info.result_code == "SUCCESS") {
        wx.showToast({
          title: '撤单成功',
          icon: 'none'
        })
      } else {
        wx.showToast({
          title: '撤单失败：' + (res.info.err_code_des ? res.info.err_code_des : res.info.return_msg),
          icon: 'none'
        })
      }
    }
    var fail = res => { }
    var comp = () => { }
    app.request(url, params, success, fail, comp);
  },
  onUnload() {
    app.globalData.isTimeout = true;
  }
})